import { FairyGUI } from "csharp";
import ObjectUtils from "./ObjectUtils";
import XML2JSON from "./XML2JSON";

/**
 * 处理XML的工具类
 * @author clong 2020/5/4 8:36
 */
export default class XMLUtils {

    /**
     * 具有复杂子几点的XML数据
     * @param xml 
     */
    public static xmlToJson2(xml: FairyGUI.Utils.XML): Object {

        if (xml == null) {
            return null;
        }
        return XML2JSON.parse(xml);
    }

    /**
     * XML文件转换为JSON对象
     * @param	xml
     * @param	obj
     * @return
     */
    public static xmlToJson(xml: FairyGUI.Utils.XML, obj: Object = null): Object {

        if (xml == null) {
            return null;
        }

        if (xml.name == null) {
            return {};
        }

        var o: Object = null;
        var name: string = xml.name.toString();
        var parent: any = null;
        if (obj == null) {
            obj = {};
            parent = obj;
            //obj[name] = setAttribe( xml , parent );	
            this.setAttribe(xml, parent);
        } else {
            parent = obj;
        }

        var len: number = xml.elements.Count;// xml.children().length();
        // var attrs: FairyGUI.Utils.XML, prop: string, value: any;
        var item: FairyGUI.Utils.XML = null;
        var i: number = 0;
        for (i = 0; i < len; i++) {
            o = {};
            //o.parent = obj;
            item = xml.elements.get_Item(i);// xml.children()[i];
            if ( !item.name ) {
                continue;
            }
            name = item.name.toString();
            //设置属性
            this.setAttribe(item, o);
            var prev: any = parent[name];
            if (!prev) {
                parent[name] = [o];
            } else {
                parent[name].push(o);
            }
            this.xmlToJson(item, o);
        }

        return obj;
    }

    private static setAttribe(item: FairyGUI.Utils.XML, o: Object): Object {

        //var attribute:String = "ssn";
        //trace(employees.employee[1].@[attribute]);  // 789-789-7890
        if (o && item) {
            let attrs:any;//System.Collections.Generic.Dictionary$2<string, string>;
            let prop: string, value: any;
            // var attrs: FairyGUI.Utils.XML,
            // for each(attrs in item.attributes()) {
            for(attrs in item.attributes) {
                console.log("------------->setAttribe: ");
                ObjectUtils.showObject( attrs );
                
                prop = attrs.name.toString();
                value = attrs.toString();
                //转换颜色值
                //value = value.indexOf("#") != -1 ? uint(String(value).replace(/#/g, "0x")) : value;
                value = String(value).indexOf("0x") != -1 ? parseInt(value).toExponential(16) : value;
                o[prop] = (value == "true" ? true : (value == "false" ? false : value));
            }
		}
        return o;
    }

    /**
     * 遍历XML的每一个节点
     * @param source 源数据XML
     * @param singleXMLFunc 处理单个节点XML的方法，将传入三个参数[ 当前节点XML,父节点XML,当前节点对应父节点的索引]
     * @param curXML 当前遍历XML
     * @param parent 父节点XML
     * @return
     */
    public static ergodicEveryXMLNode(source: FairyGUI.Utils.XML, singleXMLFunc:Laya.Handler = null, curXML: FairyGUI.Utils.XML = null, parent: FairyGUI.Utils.XML = null): void {

        if (source == null) {
            return;
        }

        if (parent == null) {
            curXML = source;
        } else {
            curXML = parent;
        }

        var i: number;
        var x: FairyGUI.Utils.XML;
        for (i = 0; i < curXML.elements.Count; i++) {
            x = curXML.elements.get_Item(i);// curXML.children()[i];
            if (x == null) {
                continue;
            }

            //执行单条XML的逻辑
            if (singleXMLFunc != null) {
                // singleXMLFunc.apply(null, [x, curXML, i]);
                singleXMLFunc.runWith( [x, curXML, i] );
            }

            //继续搜寻下一个XML条目的节点,这个子节点转换为父节点继续遍历！
            if (x.elements.Count > 0) { //有子节点
                this.ergodicEveryXMLNode(source, singleXMLFunc, null, x);
            }
        }
    }

}
